package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"lianxi3/cache"
	"lianxi3/model"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	car_name := c.PostForm("car_name")
	car_code := c.PostForm("car_code")
	if car_name == "" || car_code == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	car := model.Car{
		CarName: car_name,
		CarCode: car_code,
	}
	err := model.DB.Create(&car).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "车辆信息添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "车辆信息添加成功",
	})
	return
}
func CarOrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_status := c.PostForm("order_status")
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	order := model.CarOrder{
		OrderCode:   order_code,
		OrderStatus: order_status,
		Cid:         cid,
	}
	err := model.DB.Create(&order).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "交易订单添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "交易订单添加成功",
	})
	return
}
func CarOrderList(c *gin.Context) {
	/*
		SELECT
		  car_orders.order_code AS OrderCode,
		  car_orders.order_status AS OrderStatus,
		  car_orders.id AS OrderId,
		  cars.car_name AS CarName,
		  cars.car_code AS CarCode
		FROM
		  car_orders
		  LEFT JOIN cars ON cars.id = car_orders.cid
	*/
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var total int64
	model.DB.Model(&model.CarOrder{}).Count(&total)

	var data []model.CarOrderList
	redis_key := "order_list_" + strconv.Itoa(int(page))
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("car_orders").
			Select(
				"car_orders.order_code AS OrderCode",
				"car_orders.order_status AS OrderStatus",
				"car_orders.id AS OrderId",
				"cars.car_name AS CarName",
				"cars.car_code AS CarCode",
			).
			Joins("LEFT JOIN cars ON cars.id = car_orders.cid").
			Offset(offset).Limit(size).Debug().Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"total": total,
		"data":  data,
	})
	return
}
func CarOrderDetail(c *gin.Context) {
	/*
		SELECT
		  car_orders.order_code AS OrderCode,
		  car_orders.order_status AS OrderStatus,
		  car_orders.id AS OrderId,
		  cars.car_name AS CarName,
		  cars.car_code AS CarCode
		FROM
		  car_orders
		  LEFT JOIN cars ON cars.id = car_orders.cid
		  WHERE car_orders.id=1
	*/
	id := c.Query("id")

	var data model.CarOrderList
	redis_key := "order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("car_orders").
			Select(
				"car_orders.order_code AS OrderCode",
				"car_orders.order_status AS OrderStatus",
				"car_orders.id AS OrderId",
				"cars.car_name AS CarName",
				"cars.car_code AS CarCode",
			).
			Joins("LEFT JOIN cars ON cars.id = car_orders.cid").
			Where("car_orders.id=?", id).
			Debug().Find(&data)
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &data)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": data,
	})
	return
}
